//
//  NoticeViewController.m
//  wtshoppingcite
//
//  Created by TW on 15-1-12.
//  Copyright (c) 2015年 TW. All rights reserved.
//

#import "NoticeViewController.h"
#import "NoticeTableView.h"
#import "NoticeCell.h"
#import "CommunityModel.h"
#import "NoticeDetailViewController.h"

@interface NoticeViewController ()<UITableViewDelegate,UITableViewDataSource,UITableViewEventsDelegate>
{
    NoticeTableView *noticeTableView;
    NSString *pageNum;
}
@end

@implementation NoticeViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self setTitleView:@"物业通知"];
    
    noticeTableView = [[NoticeTableView alloc]initWithFrame:CGRectMake(0, 0, kWidth, kHeight) style:UITableViewStyleGrouped];
    noticeTableView.isNeedTorefresh = YES;
    noticeTableView.eventsDelegate = self;
    noticeTableView.isMore = YES;
    [self.view addSubview:noticeTableView];
    
    _dataArray = [[NSMutableArray alloc]initWithCapacity:1];
    
}

#pragma mark getNearkgrouplist 三公里商圈
-(void)getCommunitylist:(BOOL)isMore{
    NSString *page = pageNum;
    NSString *pageSize = @"10";
    NSString *cid = [[RegisterHelper shareHelper]citesId];
    
    NSMutableDictionary *listDic = [NSMutableDictionary dictionary];
    SetValueForKey(listDic, cid, @"cid");
    SetValueForKey(listDic, page, @"page");
    SetValueForKey(listDic, pageSize, @"pageSize");
    
    [self showLoadImageView:self.view subview:noticeTableView];
    [NetWorkService byRequestWithAPIPrefix:kBaseAppUrl
                                                  andAPISuffix:kAdvise
                                                 andHttpMethod:@"POST"
                                                 andParameters:listDic
                                               andRequestCache:NO
                                                andUseParamDic:YES
                                           andPostDataEncoding:MKNKPostDataEncodingTypeJSON
                                         andRequestFinishBlock:^(BYRequest *request, BYResponse *response) {
                                             [noticeTableView doneLoadingTableViewData];
                                             [self performSelector:@selector(dismissLoadImageView) withObject:nil afterDelay:0.5];
                                             
                                             request.requestCache = YES;
                                             
                                             if (response.isError) {
                                                 
                                                 DLog(@"error %@",response.errorMessage);
                                                 
                                             }else{
                                                 
                                                 NSDictionary *responseDic = request.responseJSON;
                                                 DLog(@"成功数据:%@",responseDic);
                                                 
                                                 NSArray *list = [responseDic objectForKey:@"list"];
                                                 if (list.count < 10) {
                                                     noticeTableView.isMore = NO;
                                                 }else{
                                                     noticeTableView.isMore = YES;
                                                     
                                                     int currentPage = [pageNum intValue];
                                                     pageNum = [NSString stringWithFormat:@"%d",currentPage+1];
                                                     
                                                 }
                                                 if (!isMore) {
                                                     [_dataArray removeAllObjects];
                                                 }
                                                 for (NSDictionary *dic in list) {
                                                     CommunityModel *shoppingModel = [[CommunityModel alloc]initWithCommunityList:dic];
                                                     [_dataArray addObject:shoppingModel];
                                                 }
                                                 noticeTableView.data = _dataArray;
                                             }
                                             [noticeTableView reloadData];
                                         }];
}


#pragma mark  eventsDelegate
- (void)pullUp:(BaseTableView *)baseTableView{
    
    [self getCommunitylist:YES];
    
}

- (void)pullDown:(BaseTableView *)baseTableView{
    
    pageNum = @"0";
    [self getCommunitylist:NO];
    
}

- (void)basetableView:(BaseTableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    CommunityModel *communiytModel = _dataArray[indexPath.row];
    
    NoticeDetailViewController *noticeDetailVC = [[NoticeDetailViewController alloc]init];
    noticeDetailVC.hidesBottomBarWhenPushed = YES;
    noticeDetailVC.communityId = communiytModel.cid;
    [self.navigationController pushViewController:noticeDetailVC animated:YES];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    
    pageNum = @"0";
    [self pullDown:noticeTableView];
    
    ;
}


- (void)viewWillDisappear:(BOOL)animated
{
    
    [super viewWillDisappear:animated];
    
    
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
